package com.skydonkey.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.skydonkey.dao.AccountFollowsMapper;
import com.skydonkey.dao.AccountInfoMapper;
import com.skydonkey.dao.AccountMapper;
import com.skydonkey.pojo.po.Account;
import com.skydonkey.pojo.po.AccountFollows;
import com.skydonkey.pojo.po.AccountInfo;
import com.skydonkey.pojo.po.AccountInfoDetails;
import com.skydonkey.pojo.vo.*;
import com.skydonkey.service.AccountInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
public class AccountInfoServiceImpl implements AccountInfoService {

    @Autowired
    AccountInfoMapper accountInfoMapper;

    @Autowired
    private AccountFollowsMapper accountFollowsMapper;

    @Autowired
    AccountMapper accountMapper;

    @Override
    public AccountInfo getAccountInfo(Long uid) {
        return accountInfoMapper.findById(uid);
    }

    @Override
    public AccountInfoResp updateAccountInfo(AccountInfoReq req) {
        AccountInfo accountInfo = new AccountInfo();
        BeanUtils.copyProperties(req, accountInfo);
        Account account = new Account();
        BeanUtils.copyProperties(req, account);
        accountMapper.updateNickname(account);
        accountInfoMapper.update(accountInfo);

        Account newAccount = accountMapper.findById(req.getUid());
        AccountInfo newInfo = accountInfoMapper.findById(req.getUid());
        AccountInfoResp resp = new AccountInfoResp();
        BeanUtils.copyProperties(newAccount, resp);
        BeanUtils.copyProperties(newInfo, resp);
        return resp;
    }

    @Override
    public int addAccountInfo(AccountInfo accountInfo) {
        return accountInfoMapper.insert(accountInfo);
    }



    // 获取关注我的人的uid
    @Override
    public PageInfo<Long> getFollowersByPage(Long followUid, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Long> followers = accountFollowsMapper.getFollowers(followUid);
        return new PageInfo<>(followers);
    }

    // 获取我关注的人的uid
    @Override
    public PageInfo<Long> getFollowingUsersByPage(Long uid, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Long> followingUsers = accountFollowsMapper.getFollowingUsers(uid);
        return new PageInfo<>(followingUsers);
    }

    // 查询当前播放视频的用户的简单信息
    @Override
    public SimpleVideoUserInfoResp getSimpleVideoUserInfo(Long uid) {
        int followsCount = accountFollowsMapper.getFollowerCount(uid);
        AccountInfo accountInfo = accountInfoMapper.findById(uid);
        if (accountInfo == null) {
            return null;
        }
        Account account = accountMapper.findById(uid);
        if (account == null) {
            return null;
        }
        SimpleVideoUserInfoResp resp = new SimpleVideoUserInfoResp();
        BeanUtils.copyProperties(accountInfo, resp);
        BeanUtils.copyProperties(account, resp);
        resp.setFollowsCount(followsCount);
        return resp;
    }

    @Override
    public AddFollowResp addFollow(AddFollowReq req) {
        AccountFollows accountFollows = new AccountFollows();
        BeanUtils.copyProperties(req, accountFollows);
        int i = accountFollowsMapper.addFollow(accountFollows);
        return i == 0 ? null : new AddFollowResp();
    }

    @Override
    public DelFollowResp delFollow(DelFollowReq req) {
        int i = accountFollowsMapper.removeFollow(req.getUid(), req.getFollowUid());
        return i == 0 ? null : new DelFollowResp();
    }

    @Override
    public GetFollowingResp getFollows(GetFollowingReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageCount());
        Page<AccountInfoDetails> details = accountFollowsMapper.getFollowingUserInfoDetails(req.getUid());
        return new GetFollowingResp().setUsers(details).setTotalCount(details.getTotal());
    }

    @Override
    public GetFollowerResp getFollowers(GetFollowerReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageCount());
        Page<AccountInfoDetails> details = accountFollowsMapper.getFollowerUserInfoDetails(req.getUid());
        return new GetFollowerResp().setUsers(details).setTotalCount(details.getTotal());
    }


}
